package io.zcoder.innoway.fragments;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.activities.SetGoalActivity;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.activities.BaseActivity;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.Dashboard;
import io.zcoder.innoway.models.DashboardResponse;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.models.ProfileDetailResponse;
import io.zcoder.innoway.rest.RestClient;
import io.zcoder.innoway.widgets.TextViewCustom;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class GoalFragment extends BaseFragment {

    public static final int REQUEST_SET_GOAL = 123;

    @BindView(R.id.current_weight)
    TextViewCustom mWeightCurrent;
    @BindView(R.id.weight_unit)
    TextViewCustom mWeightUnit;
    @BindView(R.id.weight_goal)
    TextViewCustom mWeightGoal;
    @BindView(R.id.idealText)
    TextViewCustom mIdealText;
    @BindView(R.id.you_try_to)
    TextViewCustom mTryText;

    @OnClick(R.id.reset)
    void reset() {
        if (mProfile == null || mDashboard == null) {
            return;
        }
        Intent intent = new Intent(getActivity(), SetGoalActivity.class);
        intent.putExtra(Constants.WEIGHT_EXTRAS, mDashboard.getWeight(mProfile.getWeight_unit()));
        intent.putExtra(Constants.UNIT_EXTRAS, mProfile.getWeight_unit());
        intent.putExtra(Constants.IDEAL_EXTRAS, mIdealText.getText().toString());
        intent.putExtra(Constants.GOAL_EXTRAS, mProfile.getGoal_weight());
        startActivityForResult(intent, REQUEST_SET_GOAL);
    }

    private Dashboard mDashboard;
    private Profile mProfile;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_goal_fragment, container, false);
        ButterKnife.bind(this, view);
        setTitle(getString(R.string.goal));
        getEntry();

        return view;

    }

    private void getEntry() {
        RestClient.build(getActivity(), true).getDashboard(PrefUtils.getInt(getActivity(), PrefUtils.PREF_PROFILE_ID), new Callback<DashboardResponse>() {
            @Override
            public void success(DashboardResponse dashboardResponse, Response response) {
                if (dashboardResponse.getStatus_code() != 200) {
                    ((BaseActivity) getActivity()).showAlert(getString(R.string.error), dashboardResponse.getMessage());
                } else {
                    mDashboard = dashboardResponse.getData();
                    getProfile();
                }
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });
    }

    private void getProfile() {
        RestClient.build(getActivity(), true).getProfile(PrefUtils.getInt(getActivity(), PrefUtils.PREF_PROFILE_ID), new Callback<ProfileDetailResponse>() {
            @Override
            public void success(ProfileDetailResponse profileDetailResponse, Response response) {
                if (profileDetailResponse.getStatus_code() != 200) {
                    ((BaseActivity) getActivity()).showAlert(getString(R.string.error), profileDetailResponse.getMessage());
                } else {
                    mProfile = profileDetailResponse.getData();
                    setViews();
                }
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });
    }

    private void setViews() {
        if (mDashboard == null || mProfile == null) {
            return;
        }
        mWeightCurrent.setText(mDashboard.getWeight(mProfile.getWeight_unit()) + "");
        mWeightUnit.setText(Utils.getUnitWeight(mProfile.getWeight_unit()));
        mWeightGoal.setText(mProfile.getGoalWeight(mProfile.getWeight_unit()) + "");
        float f = mProfile.getHeight() * mProfile.getHeight() / 10000;
        float idealFrom = Constants.IDEAL_BMI_FROM * f * Utils.getRatioWeight(mProfile.getWeight_unit());
        float idealTo = Constants.IDEAL_BMI_TO * f * Utils.getRatioWeight(mProfile.getWeight_unit());
        String yourIdeal = String.format(getString(R.string.your_ideal_weight), Utils.formatFloat(idealFrom) + "", Utils.formatFloat(idealTo) + Utils.getUnitWeight(mProfile.getWeight_unit()));
        mIdealText.setText(yourIdeal);
        float weight = Math.abs(mProfile.getGoalWeight(mProfile.getWeight_unit()) - mDashboard.getWeight(mProfile.getWeight_unit()));
        String tryText = getString(R.string.you_should_try_to_keep);
        if (mDashboard.getWeight() < mProfile.getGoal_weight()) {
            tryText = String.format(getString(R.string.you_should_try_to_gain), Utils.formatFloat(weight) + Utils.getUnitWeight(mProfile.getWeight_unit()));
        } else if (mDashboard.getWeight() > mProfile.getGoal_weight()) {
            tryText = String.format(getString(R.string.you_should_try_to_less), Utils.formatFloat(weight) + Utils.getUnitWeight(mProfile.getWeight_unit()));
        }
        mTryText.setText(tryText);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SET_GOAL && resultCode == Activity.RESULT_OK) {
            getEntry();
        }
    }
}
